package com.dyz.swordFingerOffer;

import java.util.HashMap;

/**
 * @author: daiyizheng
 * @date: 2021/6/2 13:54
 * @description:
 */
public class MoreThanHalfNum_Solution39 {
    public static void main(String[] args) {
        int [] arr = new int[] {47,47,72,47,72,47,79,47,12,92,13,47,47,83,33,15,18,47,47,47,47,64,47,65,47,47,47,47,70,47,47,55,47,15,60,47,47,47,47,47,46,30,58,59,47,47,47,47,47,90,64,37,20,47,100,84,47,47,47,47,47,89,47,36,47,60,47,18,47,34,47,47,47,47,47,22,47,54,30,11,47,47,86,47,55,40,49,34,19,67,16,47,36,47,41,19,80,47,47,27};
        new MoreThanHalfNum_Solution39().MoreThanHalfNum_Solution(arr);

    }
    public int MoreThanHalfNum_Solution(int [] array) {
        if (array==null){
            return 0;
        }
        if(array.length==1){
            return array[0];
        }

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0;i<array.length; i++){
            if (map.containsKey(array[i])){
                map.put(array[i], map.get(array[i])+1);
            }else{
                map.put(array[i], 1);
            }
        }
        int key =0;
        int keyNum = 0;
        for(Integer k : map.keySet()){
            if(keyNum<map.get(k)){
                keyNum = map.get(k);
                key = k;
            }
        }
        return key;
    }
}
